<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Binary</title>
</head>
<body>
<script>
    function findIndex(array,key){
        var left = 0;
        var right = array.length - 1;
        // 这里必须是 <=
        while (left <= right) {
            var mid = ~~((left + right) / 2);
            console.log(right);
            console.log('mid--'+mid);
            console.log('array[mid]--'+array[mid]);
            if (array[mid] == key) {
                return mid;
            } else if (array[mid] < key) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return -1;
    }
    console.log(findIndex([1,2,3,3,4,4,5,6],4));
</script>
</body>
</html>